// pages/optional/index/index.js
const selectorRouteDatas = [{
    id: 0,
    title: "全国",
    type: "radiobox",
    key: "province",
    items: []
  },
  {
    id: 1,
    title: "年份",
    type: "radiobox",
    key: "year",
    items: [{
        id: 2024,
        title: '2024'
      },

    ]
  },
]
const {
  matchBigdata,
  volunteerCity
} = require('../../../api/api')
const {
  decodeUserInfo
} = require('../../../api/login')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    filterRouteKey: 'optionalFilters',
    selectedData: {
      province: 0,
      year: 2024
    },
    details: {},
    selectorDataList: selectorRouteDatas,
    setting: {
      width: '220rpx',
      weight: 'normal',
      color: '#808080',
      fontSize: '26rpx',
      height: '50rpx',
      padding: '0'
    },
    optionalList: [{
      icon: 'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/optional/option_icon1.png',
      title: '专业选科',
      desc: '根据喜欢院校专业，查看最优学科组合',
      url: '/pages/optional/major/index',
      color: '#944B32',
      bgColor: "#FFEEC7",
      width: '121rpx',
      height: '121rpx'
    }, {
      icon: 'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/optional/option_icon2.png',
      title: '院校选科',
      desc: '根据喜欢大学院校，查看最优学科组合',
      url: '/pages/optional/school/index',
      color: '#8B2797',
      bgColor: '#DBE5FF',
      width: '109rpx',
      height: '96rpx'
    }, {
      icon: 'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/optional/option_icon3.png',
      title: '自主选科',
      desc: '根据擅长学科分析，可报考院校及专业',
      url: '/pages/optional/choose/index',
      color: '#2D5445',
      bgColor: '#CFF4CC',
      width: '131rpx',
      height: '91rpx'
    }],
    list: [{
      name: '不限',
      num: 14298,
      percent: 34.4
    }, {
      name: '物理',
      num: 17304,
      percent: 52.4
    }, {
      name: '化学',
      num: 15578,
      percent: 47.2
    }, {
      name: '生物',
      num: 522,
      percent: 1.5
    }, {
      name: '历史',
      num: 490,
      percent: 1.4
    }, {
      name: '政治',
      num: 461,
      percent: 1.3
    }, {
      name: '地理',
      num: 283,
      percent: 0.8
    }, ],
    isSvip: false,
    isVip: false,
    typeVip: 'vip'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const {
      filterRouteKey,
      selectorDataList
    } = this.data
    var d = new Date()
    var year = d.getFullYear()
    var month = d.getMonth()
    if (month > 6) {
      year = year + 1
    }

    var len = year + 1
    var yearList = []
    for (var i = year; i < len; i++) {
      yearList.push({
        id: i,
        title: i
      })
    }
    selectorDataList[1].items = yearList
    let selectedData = {
      province: wx.getStorageSync('userCitys').code,
      year: year
    }
    let selectedData1 = {
      province: wx.getStorageSync('userCitys').code,
      year: year
    }
    this.setData({
      selectedData,
      selectorDataList
    })
    wx.setStorageSync(filterRouteKey, selectedData)
    wx.setStorageSync('optionalChooseFilters', selectedData1)
    this.fetchCity()
    this.getMatchBigdata()
  },
  onShow() {
    if (this.data.selectorDataList[0].items.length > 0) {
      this.filterKeyFunc()
    }
    this.fetchUserInfo()
  },
  async fetchUserInfo() {
    const {
      vipType
    } = await getApp().fetchUserInfo()
    this.setData({
      isSvip: vipType == 'svip' ? vipType : false,
      vipType
    })
  },
  async fetchCity() {
    const {
      type,
      selectorDataList
    } = this.data
    // const cityList = wx.getStorageSync('cityList') || await getApp().getCityList()
    const {
      result
    } = await volunteerCity()
    const cityList = result.map(e => {
      return {
        title: e.province,
        id: e.provinceId,
      }
    })
    selectorDataList[0].items = cityList
    this.setData({
      selectorDataList
    })
    this.filterKeyFunc()
  },
  filterKeyFunc() {
    const selectedData = wx.getStorageSync(this.data.filterRouteKey)
    const {
      selectorDataList
    } = this.data
    selectorDataList.map((item) => {
      const c = item.items.filter((child) => {
        return child.id == selectedData[item.key]
      })
      item.title = c.length > 0 ? c[0].title : item.title
    })
    this.setData({
      selectorDataList,
      selectedData
    })
  },
  // 获取页面数据
  async getMatchBigdata() {
    const {
      selectedData
    } = this.data
    const parmas = {
      provinceId: selectedData.province,
      year: selectedData.year,
    }
    console.log(selectedData)
    const {
      result
    } = await matchBigdata(parmas)
    let omax
    let tmax
    if (result.schoolNum) {
      omax = Math.max.apply(Math, result.subjectList.map(e => {
        return e.num
      }))
      tmax = Math.max.apply(Math, result.subjectGroupList.map(e => {
        return e.num
      }))
      result.subjectList.sort((a, b) => {
        return b.num - a.num
      })
      result.subjectGroupList.sort((a, b) => {
        return b.num - a.num
      })
      result.subjectGroupList.map(e => {
        e.subject = e.subject.replace(/_/g, "+");
      })
    }
    this.setData({
      details: result,
      omax,
      tmax
    })
  },
  async onSelectChangedRoute(e) {
    wx.setStorageSync(this.data.filterRouteKey, {
      ...e.detail
    });
    this.filterKeyFunc()
    this.setData({
      ...e.detail
    });
    this.getMatchBigdata()
  },
  goto(e) {
    const {
      url
    } = e.currentTarget.dataset
    wx.navigateTo({
      url,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '新高考选科工具',
      imageUrl: 'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/users/shareImg.jpg'
    }
  }
})